package sk.ygor.raynor.client.json.basetype;

import com.google.gwt.json.client.JSONString;
import com.google.gwt.json.client.JSONValue;
import sk.ygor.raynor.client.json.JSONSerializationException;

public class java_lang_Character_JSONSerializer extends BaseTypeSerializer<Character> {

    @Override
    protected Character deserializeNonNullValue(JSONValue jsonValue) {
        final JSONString jsonString = jsonValue.isString();
        if (jsonString != null) {
            final String stringValue = jsonString.stringValue();
            if (stringValue.length() == 1) {
                return stringValue.charAt(0);
            }
        }
        throw new JSONSerializationException("Cannot create String from: " + jsonValue);
    }

    @Override
    protected JSONValue serializeNonNullValue(Character javaValue) {
        return new JSONString(javaValue.toString());
    }

}